package org.boot.easyapi.core.controller.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.boot.easyapi.core.service.api.ApiService;
import org.boot.easyapi.pojo.web.CommonResult;
import org.boot.easyapi.pojo.web.PageResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static org.boot.easyapi.pojo.web.CommonResult.success;

@Slf4j
@RestController
@RequestMapping("/easyapi/api")
@Tag(name = "EasyApi - 系统表接口")
public class ApiController {

    @Resource
    private ApiService apiService;

    @Operation(summary = "数据新增")
    @PostMapping("/{table}/insert")
    public CommonResult<Long> insert(@PathVariable String table,
                                     @RequestBody Map<String, Object> param) {
        return success(apiService.insert(table, param));
    }

    @Operation(summary = "数据批量新增")
    @PostMapping("/{table}/insertBatch")
    public CommonResult<int[]> batchInsert(@PathVariable String table,
                                           @RequestBody List<Map<String, Object>> param) {
        return success(apiService.insertBatch(table, param));
    }

    @Operation(summary = "数据更新")
    @PutMapping("/{table}/update")
    public CommonResult<Integer> update(@PathVariable String table,
                                        @RequestBody Map<String, Object> param) {
        return success(apiService.update(table, param));
    }

    @Operation(summary = "数据批量更新")
    @PutMapping("/{table}/updateBatch")
    public CommonResult<List<Integer>> batchUpdate(@PathVariable String table, 
                                                   @RequestBody List<Map<String, Object>> param) {
        return success(apiService.updateBatch(table, param));
    }

    @Operation(summary = "数据删除")
    @DeleteMapping("/{table}/delete")
    public CommonResult<Integer> delete(@PathVariable String table, 
                                        @RequestParam("id") Long id) {
        return success(apiService.delete(table, id));
    }

    @Operation(summary = "数据批量删除")
    @DeleteMapping("/{table}/deleteBatch")
    public CommonResult<int[]> batchDelete(@PathVariable String table, 
                                           @RequestParam("ids") List<Long> ids) {
        return success(apiService.deleteBatch(table, ids));
    }

    @Operation(summary = "查询数据详情")
    @GetMapping("/{table}/get")
    public CommonResult<Map<String, Object>> getOne(@PathVariable String table,
                                                    @RequestParam("id") Object id,
                                                    String respKey) {
        return success(apiService.get(table, id, respKey));
    }

    @Operation(summary = "查询数据列表")
    @GetMapping("/{table}/list")
    public CommonResult<List<Map<String, Object>>> list(@PathVariable String table,
                                                        @RequestParam Map<String, Object> param) {
        return success(apiService.list(table, param));
    }

    @Operation(summary = "查询分页数据列表")
    @GetMapping("/{table}/page")
    public CommonResult<PageResult<Map<String, Object>>> page(@PathVariable String table,
                                                              @RequestParam Map<String, Object> param) {
        return success(apiService.page(table, param));
    }

    @Operation(summary = "导出excel")
    @GetMapping("/{table}/export")
    public byte[] exportExcel(@PathVariable String table,
                              @RequestParam(name = "configId", required = false) Long configId,
                              @RequestParam Map<String, Object> params,
                              HttpServletResponse response) {
        byte[] bytes = apiService.exportExcel(table, configId, params);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + table + ".xlsx");
        return bytes;
    }

    @Operation(summary = "导入excel")
    @PostMapping("/{table}/import")
    public CommonResult<String> importExcel(@PathVariable String table,
                                            @RequestParam("file") MultipartFile file,
                                            @RequestParam("configId") Long id) {
        return success(apiService.importExcel(table, id, file));
    }

}
